# 存储和读取一堆数据
def my_write():
    # 一维数据可以使用列表 元组 集合进行存储
    lst = ['张三', '李四', '王五', '赵六']  # 一维数据
    with open('student.csv', 'w', encoding='utf-8') as file:
        file.write(','.join(lst))


# my_write()

def my_read():
    with open('student.csv', 'r', encoding='utf-8') as file:
        s = file.read()
        lst = s.split(',')  # 使用字符串截取 逗号分隔
        print(lst)


# my_read()

# 存储和读取二维数据
def my_write_table():
    lst = [
        ['商品名称', '单价', '采购数量'],
        ['水杯', '98.5', '20'],
        ['鼠标', '89', '100']
    ]  # 二维数据使用二维列表存储
    with open('table.csv', 'w', encoding='gbk') as file:
        for item in lst:
            line = ','.join(item)
            file.write(line)
            file.write('\n')


# my_write_table()

def my_read_table():
    data = []
    with open('table.csv', 'r', encoding='gbk') as file:
        lst = file.readlines()  # 结果是一个列表类型
        for item in lst:
            newlst = item[:len(item) - 1].split(',')
            # print(newlst)
            data.append(newlst)
    print(data)

my_read_table()
